#!/usr/bin/env python
import os
import sys
# COMMENT
# INSERT INTO  INSERT INTO data(date,temperature,lux) VALUES(datetime('now'),1.1,1.1);
import sqlite3
from yoctopuce.yocto_api import *
from yoctopuce.yocto_temperature import *
from yoctopuce.yocto_lightsensor import *

# Setup the API to use local USB devices
errmsg= YRefParam()  # little trick for By Ref parameters

if YAPI.RegisterHub("usb", errmsg)!= YAPI.SUCCESS:
    sys.exit("init error"+str(errmsg))

tempSensor = YTemperature.FirstTemperature()

if tempSensor is None :
    sys.exit("No temperature sensor")

lightSensor = YLightSensor.FirstLightSensor()
if lightSensor is None :
    sys.exit("No light sensor")

temperature = (tempSensor.get_currentValue())
lux = (lightSensor.get_currentValue())

conn = sqlite3.connect("/var/indoor-weather/indoor-weather.db")
c = conn.cursor()
c.execute("INSERT INTO data(date,temperature,lux) VALUES(datetime('now','localtime'),?,?)", (temperature,lux))
conn.commit()
conn.close()

